package com.financing.controller.user;
import java.util.Date;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;

import com.financing.model.Data;
import com.financing.model.user.Staff;
import com.financing.model.user.User;
import com.financing.service.system.LoggerService;
import com.financing.service.user.StaffService;
import com.financing.util.Json;
import com.financing.util.MD5Util;
import com.financing.util.Str;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

/**
 *  员工控制器
 * @since 2017-06-06
 * @author 无处不在的二叔
 */
public class StaffController extends Controller {
	private StaffService staff = new StaffService();
	private LoggerService log = new LoggerService();

	/**
	 *  获取员工信息
	 */ 
	public void info() {
		Staff s =  Staff.me.findById(getParaToInt("id",0));
		// 获取上级
		Staff leader = staff.findByUserId(s.getLeader());
		s.put("leaderName", leader!=null?leader.getRealName():"无上级");
		renderJson(Json.ok("数据获取成功！",s));
	}
	
	/**
	 * 员工列表
	 */
	public void list() { 
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		Page<Staff> p = staff.list(page,size,Data.flat(getParaMap()));
		renderJson(Json.ok( "数据获取成功！", Data.put("page",p)));
	}	
	
	/**
	 * 员工信息编辑
	 */
	public void edit() { 
		User u = getAttr("user");
		int userId = getParaToInt("userId",0);
		int leaderId = getParaToInt("leader",0);
		String mobile = getPara("mobile");
		String realName = getPara("realName",mobile);
		String role = getPara("role");
		String groups = getPara("groups");
		String workcode = getPara("workcode");
		String password = getPara("password");
		
		// 新建员工判断
		if(userId==0) {
			if(Str.eq("agent_leader", role)) {
				if(staff.list(1,1,Data.putStr("role",role)).getList().size()>0) {
					renderJson(Json.error("当前客服组已有组长！"));
					return ;
				}
			} 
			User u1 = new User();
			u1.setRealName(realName)
				.setMobile(mobile)
				.setPassword(MD5Util.encrypt(password))
				.save();
			userId = u1.getId();
		}
		
		Staff s = staff.findByWorkcode(workcode);
		if(staff.findByWorkcode(workcode)!=null && s.getUserId()!=userId) {
			renderJson(Json.error("此工号已存在！"));
			return ;
		} 
		
		if(userId>0 && Str.nb(realName) && Str.nb(mobile)){
			int id = getParaToInt("id",0);
			s = id>0 ? Staff.me.findById(id) : new Staff();
			s.setUserId(userId);
			s.setRealName(realName);
			s.setMobile(mobile);
			s.setWorkcode(workcode);
			s.setRole(role);
			s.setGroups(groups);
			s.setTarget(getParaToInt("target",0));
			if(Str.nb(password)) {
				s.setPassword(MD5Util.encrypt(password));
			}
			
			// 获取员工所有上级
			if(leaderId>0) {
				s.setLeader(leaderId);
				s.setLeaders(staff.findLeaders(userId, leaderId)); 
			}
			
			if(id>0 ? s.setUpdateTime(new Date()).update() : s.setCreateTime(new Date()).save()){
				// 插入日志
				log.build("员工编辑", "编辑员工"+realName+"的信息", u.getId(), "backend");
				renderJson(Json.ok("员工信息编辑成功！")); 
			} else {
				renderJson(Json.error("员工信息编辑失败！"));
			}
		} else {
			renderJson(Json.error("员工信息不可为空！"));
		}
	}	
	
	/**
	 * 客服绩效信息修改
	 */
	@RequiresRoles(logical=Logical.OR,value={"admin","agent_leader","agent_manager"})  
	public void setTarget() { 
		Staff s = Staff.me.findById(getParaToInt("id",0));
		if(s!=null) {
			s.setTarget(getParaToInt("target",0)).update();
			renderJson(Json.ok("客服绩效信息修改成功！"));
		} else {
			renderJson(Json.error("客服信息不存在！"));
		}
	}
}
 